package top.csy8888.deadqueue.service;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Service;
import top.csy8888.deadqueue.consts.RabbitConstants;

import java.io.IOException;
import java.util.Map;

/**
 * @author CSY
 * @date 2025 - 10 - 16
 */
@Service
@Slf4j
public class DeadConsumerService {
    @RabbitListener(queues = RabbitConstants.DEAD_QUEUE_NAME)
    public void receiveDeadLetter(String message, Channel channel, @Headers Map<String, Object> headers) {
        log.info("接收到异常消费信息，转人工处理：{}",message);
        long tag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicReject(tag,false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
